package teach.aistar.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import teach.aistar.model.entity.Admin;
import teach.aistar.model.entity.User;
import teach.aistar.model.vo.Result;
import teach.aistar.service.IAdminService;
import teach.aistar.service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class AdminController {

    //登录
    @GetMapping("/adminLogin")
    public String logins(){
        return "jsp/backend/auth-sign-in";
    }

    @Autowired
    IAdminService iAdminService;

    @PostMapping("/loginZ")
    @ResponseBody
    public Result register(@Validated Admin admin, BindingResult bindingResult, HttpServletRequest req, HttpSession session) {


        List<FieldError> fieldErrorList = bindingResult.getFieldErrors();






        Map<String, String> errorsMap = new HashMap<>();
        if (null != fieldErrorList && fieldErrorList.size() > 0) {
            for (FieldError f : fieldErrorList) {
                System.out.print(f.getField() + "=>");
                System.out.println(f.getDefaultMessage());
                errorsMap.put(f.getField(), f.getDefaultMessage());
            }
            return new Result("500", "登录失败", errorsMap);
        } else {
//
//            this.aid = aid;
//            this.adName = adName;
//            this.adPhone = adPhone;
//            this.adAccount = adAccount;
//            this.adPassword = adPassword;
//            this.adCreateDate = adCreateDate;
            String uname = admin.getAdName();

            System.out.println("管理员姓名:"+uname);
            String pwd = admin.getAdPassword();
            System.out.println(uname);
            System.out.println(pwd);
            Admin admin1 = iAdminService.login(uname);
            System.out.println(admin1);

            if (admin1 != null) {
                if (admin1.getAdPassword().equals(pwd)) {
                    System.out.println(admin1.getAdPassword());
                    System.out.println(pwd);
//                    model.addAttribute("uname",user1.getUname());
                    req.getSession().setAttribute("uname", admin1.getAdName());
                    req.setAttribute("uname", admin1.getAdName());
                    req.setAttribute("uId", admin1.getAid());
                    session.setAttribute("Admin", admin1);
                    return new Result("200", "登录成功", null);
                } else {
                    return new Result("401", "密码错误", null);
                }
            } else {
                return new Result("402", "用户名错误", null);
            }
        }
    }}






